<?php

class User extends CActiveRecord {
    const STATUS_NOACTIVE=0;
    const STATUS_NOACTIVE_ICO = '/images/fff/time.png';
    const STATUS_ACTIVE=1;
    const STATUS_ACTIVE_ICO = '/images/fff/accept.png';
    const STATUS_BANED=-1;
    const STATUS_BANED_ICO = '/images/fff/lock.png';

    const S_LOCKED=1;
    const S_UNLOCKED=0;

    const S_SUPPLIER = 3;
    const S_TRANSPORTER = 2;
    const S_DISTRIBUTOR = 4;
    const S_DEFAULT_USER = 1;
    const S_ADMINISTRATOR = 5;

    const S_IMAGES = '/media/images/';
    const S_THUMBNAIL = '/media/images/thumbnails/';
    const S_NOIMAGE = 'noimage.gif';
    


    /**
     * The followings are the available columns in table 'users':
     * @var integer $id
     * @var string $username
     * @var string $password
     * @var string $email
     * @var string $activkey
     * @var integer $createtime
     * @var integer $lastvisit
     * @var integer $superuser
     * @var integer $status
     * @var integer $parent
     * @property integer $modified_time
     * @property string $phone
     * @property string $mobile
     * @property string $avatar
     * @property string $address
     * @property integer $province
     * @property integer $lock_from
     * @property integer $birthday
     * @property integer $gender
     * @property string $web
     * @property string $full_name
     */

    /**
     * Returns the static model of the specified AR class.
     * @return CActiveRecord the static model class
     */
    public static function model($className=__CLASS__) {
        return parent::model($className);
    }

    /**
     * @return string the associated database table name
     */
    public function tableName() {
        return Yii::app()->getModule('user')->tableUsers;
    }

    /**
     * @return array validation rules for model attributes.
     */
    public function rules() {
        // NOTE: you should only define rules for those attributes that
        // will receive user inputs.

        return ((Yii::app()->getModule('user')->isAdmin()||Yii::app()->getModule('user')->isSupplier()) ? array(
            #array('username, password, email', 'required'),
            array('username', 'length', 'max' => 20, 'min' => 3, 'message' => UserModule::t("Incorrect username (length between 3 and 20 characters).")),
            array('password', 'length', 'max' => 128, 'min' => 4, 'message' => UserModule::t("Incorrect password (minimal length 4 symbols).")),
            array('email', 'email'),
            array('username', 'unique', 'message' => UserModule::t("This user's name already exists.")),
            array('email', 'unique', 'message' => UserModule::t("This user's email address already exists.")),
            array('username', 'match', 'pattern' => '/^[A-Za-z0-9_]+$/u', 'message' => UserModule::t("Incorrect symbols (A-z0-9).")),
            array('status', 'in', 'range' => array(self::STATUS_NOACTIVE, self::STATUS_ACTIVE, self::STATUS_BANED)),
            array('superuser', 'in', 'range' => array(1, 2, 3, 4, 5)),
            array('gender', 'in', 'range' => array(0, 1)),
            array('address', 'length', 'max' => 255, 'min' => 0),
            array('phone, mobile', 'length', 'max' => 45, 'min' => 0),
            array('full_name', 'length', 'max' => 255, 'min' => 3),
            array('web', 'length', 'max' => 255),
            array('username, email, createtime,full_name, phone, mobile, lastvisit, superuser, status', 'required'),
            array('createtime,lock_from,business, method_tran,birthday,modified_time,province,lastvisit, superuser, status', 'numerical', 'integerOnly' => true),
            array('avatar', 'file',
                'types' => 'gif, jpg, png',
                'maxSize' => 1024 * 1024 * 2,
                'tooLarge' => 'The file was larger than 2MB. Please upload a smaller file.',
                'allowEmpty' => true,
            ),
                ) : ((Yii::app()->user->id == $this->id) ? array(
                    array('username, email', 'required'),
                    array('username', 'length', 'max' => 20, 'min' => 3, 'message' => UserModule::t("Incorrect username (length between 3 and 20 characters).")),
                    array('email', 'email'),
                    array('username', 'unique', 'message' => UserModule::t("This user's name already exists.")),
                    array('username', 'match', 'pattern' => '/^[A-Za-z0-9_]+$/u', 'message' => UserModule::t("Incorrect symbols (A-z0-9).")),
                    array('email', 'unique', 'message' => UserModule::t("This user's email address already exists.")),
                        ) : array()));
    }

    /**
     * @return array relational rules.
     */
    public function relations() {
        $relations = array(
            'profile' => array(self::HAS_ONE, 'Profile', 'user_id'),
        );
        if (isset(Yii::app()->getModule('user')->relations))
            $relations = array_merge($relations, Yii::app()->getModule('user')->relations);
        return $relations;
    }

    /**
     * @return array customized attribute labels (name=>label)
     */
    public function attributeLabels() {
        return array(
            'username' => UserModule::t("username"),
            'password' => UserModule::t("password"),
            'verifyPassword' => UserModule::t("Retype Password"),
            'email' => UserModule::t("E-mail"),
            'verifyCode' => UserModule::t("Verification Code"),
            'id' => UserModule::t("Id"),
            'activkey' => UserModule::t("activation key"),
            'createtime' => UserModule::t("Registration date"),
            'lastvisit' => UserModule::t("Last visit"),
            'superuser' => UserModule::t("Superuser"),
            'avatar' => UserModule::t("Ảnh đại diện"),
            'phone' => UserModule::t("Phone"),
            'mobile' => UserModule::t("Mobile"),
            'address' => UserModule::t("Địa chỉ"),
            'province' => UserModule::t("Tỉnh/Thành phố"),
            'gender' => UserModule::t("Giới tính"),
            'full_name' => UserModule::t("Tên đầy đủ"),
            'lock_from' => UserModule::t("Khóa từ"),
            'web' => UserModule::t("Trang web"),
            'modified_time' => UserModule::t("Sửa lúc"),
            'birthday' => UserModule::t("Ngày sinh"),
            'business' => UserModule::t("Sản phẩm cung cấp"),
            'method_tran' => UserModule::t("Loại hình vận chuyển"),
        );
    }

    public function scopes() {
        return array(
            'active' => array(
                'condition' => 'status=' . self::STATUS_ACTIVE,
            ),
            'notactvie' => array(
                'condition' => 'status=' . self::STATUS_NOACTIVE,
            ),
            'banned' => array(
                'condition' => 'status=' . self::STATUS_BANED,
            ),
            'administrator' => array(
                'condition' => 'superuser=' . self::S_ADMINISTRATOR,
            ),
            'supplier' => array(
                'condition' => 'superuser=' . self::S_SUPPLIER,
            ),
            'transporter' => array(
                'condition' => 'superuser=' . self::S_TRANSPORTER,
            ),
            'distributor' => array(
                'condition' => 'superuser=' . self::S_DISTRIBUTOR,
            ),
            'defaultUser' => array(
                'condition' => 'superuser=' . self::S_DEFAULT_USER,
            ),
            'notsafe' => array(
                'select' => 'id, username, password, email, activkey, createtime,
                            lastvisit, superuser, status, avatar, phone, mobile,
                            full_name, modified_time, lock_from, birthday,gender,
                            web, province, address, parent, business, method_tran
                            ',
            ),
        );
    }

    public function defaultScope() {
        return array(
            'select' => 'id, username, email, createtime, lastvisit, superuser, status,
                lastvisit, superuser, status, avatar, phone, mobile,
                            full_name, modified_time, lock_from, birthday,gender,
                            web, province, address, parent, business, method_tran
                ',
        );
    }

    public static function itemAlias($type, $code=NULL) {
        $_items = array(
            'UserStatus' => array(
                self::STATUS_NOACTIVE => UserModule::t('Not active'),
                self::STATUS_ACTIVE => UserModule::t('Active'),
                self::STATUS_BANED => UserModule::t('Banned'),
            ),
//            'UserStatus' => array(
//                self::STATUS_NOACTIVE => CHtml::image(Yii::app()->request->baseUrl.self::STATUS_NOACTIVE_ICO,'',array(
//                    'title'=>UserModule::t('Not active'),
//                )),
//                self::STATUS_ACTIVE => CHtml::image(Yii::app()->request->baseUrl.self::STATUS_ACTIVE_ICO,UserModule::t('Active'),array(
//                    'title'=>UserModule::t('Active'),
//                )),
//                self::STATUS_BANED => CHtml::image(Yii::app()->request->baseUrl.self::STATUS_BANED_ICO,UserModule::t('Banned'),array(
//                    'title'=>UserModule::t('Banned'),
//                )),
//            ),
//			'AdminStatus' => array(
//				'0' => UserModule::t('No'),
//				'1' => UserModule::t('Yes'),
//			),
            'UserType' => array(
                self::S_DEFAULT_USER => UserModule::t('Default User'),
                self::S_DISTRIBUTOR => UserModule::t('Distributor'),
                self::S_ADMINISTRATOR => UserModule::t('Administrator'),
                self::S_SUPPLIER => UserModule::t('Supplier'),
                self::S_TRANSPORTER => UserModule::t('Transporter'),
                
            ),
            'UserGender' => array(
                1 => UserModule::t('Nam'),
                0 => UserModule::t('Nữ'),
            )
        );
        if (isset($code))
            return isset($_items[$type][$code]) ? $_items[$type][$code] : false;
        else
            return isset($_items[$type]) ? $_items[$type] : false;
    }
    public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id);
		$criteria->compare('username',$this->username,true);
		$criteria->compare('password',$this->password,true);
		$criteria->compare('email',$this->email,true);
		$criteria->compare('activkey',$this->activkey,true);
		$criteria->compare('createtime',$this->createtime);
		$criteria->compare('lastvisit',$this->lastvisit);
		$criteria->compare('superuser',$this->superuser);
		$criteria->compare('status',$this->status);
		$criteria->compare('modified_time',$this->modified_time);
		$criteria->compare('phone',$this->phone,true);
		$criteria->compare('mobile',$this->mobile,true);
		$criteria->compare('avatar',$this->avatar,true);
		$criteria->compare('address',$this->address,true);
		$criteria->compare('province',$this->province);
		$criteria->compare('lock_from',$this->lock_from);
		$criteria->compare('birthday',$this->birthday);
		$criteria->compare('gender',$this->gender);
                $criteria->compare('business',$this->business);
                $criteria->compare('method_tran',$this->method_tran);
		$criteria->compare('web',$this->web,true);
		$criteria->compare('full_name',$this->full_name,true);

		return new CActiveDataProvider(get_class($this), array(
			'criteria'=>$criteria,
		));
	}

    public static function lightBoxAddUser($type) {
        Yii::import('ext.jqPrettyPhoto');
        $options = array(
            'show_title' => false,
            'default_width' => 680,
            'default_height' => 500,
            //'allow_resize' => true,
            'animation_speed' => 'fast', /* fast/slow/normal */
            'opacity' => 0.60,
        );
        jqPrettyPhoto::addPretty(' a.add-new, .button-column a.view, .button-column a.update', jqPrettyPhoto::PRETTY_SINGLE, jqPrettyPhoto::THEME_FACEBOOK, $options);
        //jqPrettyPhoto::addPretty('', jqPrettyPhoto::PRETTY_SINGLE, jqPrettyPhoto::THEME_FACEBOOK, $options);
        $l = Yii::app()->getModule('user')->createUserUrl;
        $l[0] = $l[0] . '/type/' . $type . '?iframe=true';
        $link_options = array('class' => 'add-new', 'rel' => 'prettyPhoto');
        $label = UserModule::t("Add new"); //. ' ' . User::itemAlias("UserType", $type);
        return
        CHtml::link($label, $l, $link_options);
    }

}